<?php
    set_time_limit(0);
    
    $cache = new Memcache;
    $cache->connect('localhost', 11211);
    
    $code = $_GET['code'];
    $info = $cache->get('code_' . $code);
    $files = $cache->get('files_' . $code);
    if (is_array($info) && is_array($files)) {
        $progress = '';
        $tmpFile = $info['tempPath'] . '/' . $code;
        $f = fopen($tmpFile . '.log', 'w');
        
        /**
         * функція для логування виводу у кеш
         * 
         * @global string $f
         * @param string $toLog
         * @return string порожній рядок 
         */
        function _log($toLog) {
            global $f;
            fwrite($f, $toLog);
            return "";
        }

        try {
            $path = get_include_path();
            set_include_path('/var/www/library');
            require_once 'Zend/Reflection/File.php';
            
            $includePath = implode(PATH_SEPARATOR, 
                                   $info['loader']['include']['path']);
            if ($info['loader']['include']['defaultPath'] == 'before')
                $includePath = $path . PATH_SEPARATOR . $includePath;
            elseif ($info['loader']['include']['defaultPath'] == 'after')
                $includePath .= PATH_SEPARATOR . $includePath;
            set_include_path($includePath);
            
            $function = create_function('$className', 
                                        $info['loader']['autoloader']);
            spl_autoload_register($function);
            
            $pathLength = strlen($info['project']['path']);
            $xml = new DomDocument('1.0', 'utf-8');
            $classesNode = $xml->appendChild($xml->createElement('classes'));
            foreach ($files as $file) {
                $relFile = substr($file, $pathLength);
                _log($relFile . "\n");
                require_once $file;
                $fileRef = new Zend_Reflection_File($file);
                $classes = $fileRef->getClasses();
                foreach ($classes as $class) {
                    //$class = new Zend_Reflection_Class;
                    $classNode = $classesNode->appendChild($xml->createElement('class'));
                    $classNode->setAttribute('name', $class->getName());
                    $classNode->appendChild($xml->createElement('file'))
                              ->nodeValue = $relFile;
                    if ($parent = $class->getParentClass())
                        $classNode->setAttribute('extends', $parent->getName());
                    $testCases = $classNode->appendChild($xml->createElement('testcases'));
                    $interfacesNode = $classNode->appendChild($xml->createElement('interfaces'));

                    $methods = $class->getMethods();
                    foreach ($methods as $method) {
                        //$method = new Zend_Reflection_Method;
                        $testCase = $testCases->appendChild($xml->createElement('testcase'));
                        $testCase->setAttribute('name', $method->getName());
                        $testCase->appendChild($xml->createElement('method'))
                                 ->nodeValue = $method->getName();
                    }

                    $interfaces = $class->getInterfaces();
                    foreach ($interfaces as $interface) {
                        //$interface = new Zend_Reflection_Interface;
                        $interfacesNode->appendChild($xml->createElement('interface'))
                                       ->nodeValue = $interface->getName();
                    }
                    $numMethods = count($methods);
                    $numInterfaces = count($interfaces);
                    _log("\t{$class->getName()}: {$numMethods} methods, {$numInterfaces} interfaces\n");
                }
                /*
                for ($i=0, $k=0; $i<100000; $i++) {
                    $k1 = $i + $i +$i + $i + $i;
                }
                 */
            }
            $xml->save('/tmp/1.xml');
            
            
            echo $code;
        }
        catch(Exception $ex) {
            echo $ex->getMessage();
        }    
    }
    else {
        echo 'Not enough data for operation';
    }
    $info = $cache->set('code_' . $code, 'complete');